//
// Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
//
// GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
//  Author: Shawn.Tan
//  Date : 2025.10.28
//
//  History : Initial Version 2025.10.28
//
//
#include "FieldEncoding.h"

#include <algorithm>

#include "Log.h"
#include "StringUtils.h"
/*!
  \file FieldEncoding.cc
  \brief Code for encoding related functionalities.
*/

using namespace std;

namespace Green {

  FieldEncoding::~FieldEncoding()
  {
    mpEncodingBits = nullptr;
  }

  void FieldEncoding::SetBits(const string& bits_str)
  {
    mSize = 0;
    StringSplitter ss(bits_str, ',');
    while (!ss.EndOfString()) {
      string sub_str = ss.NextSubString();
      unsigned range_low = 0, range_high = 0;
      parse_range32(sub_str, range_low, range_high);
      uint32 range_size = (range_high - range_low) + 1;
      mpEncodingBits->push_back(EncodingBits(range_low, range_size));
      mSize += range_size;
    }
  }

  uint32 FieldEncoding::Lsb() const
  {
    auto itr = min_element(mpEncodingBits->begin(), mpEncodingBits->end(),
      [](const EncodingBits& encBitsA, const EncodingBits& encBitsB) { return (encBitsA.mLsb < encBitsB.mLsb); });

    uint32 ret_lsb = 63;
    if (itr != mpEncodingBits->end()) {
      ret_lsb = itr->mLsb;
    }

    return ret_lsb;
  }

}
